<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use HasFactory, Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name',
        'email',
        'password',
        'email_token'
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];


    //关联博客
    public function blog()
    {
        return $this->hasMany(Blogs::class);
    }


    //关注的人
    public function following()
    {
        return $this->belongsToMany(User::class, 'follows', 'user_id', 'follower');
    }

    //关注我的人
    public function followed()
    {
        return $this->belongsToMany(User::class, 'follows', 'follower', 'user_id');
    }

    public function isfollowing($uid)
    {
        return $this->following()->wherePivot('follower', $uid)->first();
    }

    public function followToggle($ids)
    {
        $ids = is_array($ids) ?: [$ids];
        return $this->following()->toggle($ids);
    }
}
